package com.starbaba.core.utils.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
 * IO 工具类，已实现file和ftp协议
 *
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2011-12-31 上午12:23:26
 * @version V1.0
 */
@SuppressWarnings("all")
public class IOUtilFactory {
	
//	public static final String SYSTEM_FILE_SEPARATOR = System.getProperty("file.separator");
	public static final String SYSTEM_FILE_SEPARATOR = File.separator;
	
	
	public static String PROTOCOL_FILE = "file://";
	
	public static String PROTOCOL_FTP = "ftp://";
	
	public static String PROTOCOL_HTTP = "http://";
	
	private static IOUtilFactory INSTANCE = null;
	
	
	private Map STRATEGY_MAP = new HashMap();
	
	public IOUtilFactory() {
		STRATEGY_MAP.put(PROTOCOL_FILE, new FileIOUtil());
		STRATEGY_MAP.put(PROTOCOL_FTP, new FtpIOUtil());
		
//		MapUtils.debugPrint(System.out, "实例化", STRATEGY_MAP);
	}
	
	

	public static IOUtilFactory getInstance() {
		if(INSTANCE == null) {
			INSTANCE = new IOUtilFactory(); 
		}
		
		return INSTANCE;
	}
	
	@SuppressWarnings("static-access")
	public static BaseIOUtil getIOUtil(String prococol) {
//		MapUtils.debugPrint(System.out, "读取", getInstance().STRATEGY_MAP);
		return (BaseIOUtil) getInstance().STRATEGY_MAP.get(prococol);
	}
	
	/**
	 * 打开本地或者远程目标文件，若目标文件父目录不存在，则创建，最后打开目标文件输出流
	 * 
	 * @param urlSavePath 本地或者远程目标文件目录
	 * @param filename 目标文件名
	 * @return OutputStream 目标文件输出流
	 * @throws Exception
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-12-31 上午12:23:58
	 * @version V1.0
	 */
	public static OutputStream openOutputStream(String urlSavePath, String filename) throws Exception {
		if(StringUtils.startsWithIgnoreCase(urlSavePath, PROTOCOL_FTP)) {
			//使用FTP协议输出文件
			return getIOUtil(PROTOCOL_FTP).openOutputStream(urlSavePath, filename);
		} else {
			return getIOUtil(PROTOCOL_FILE).openOutputStream(urlSavePath, filename);
		}
	}
	
	/**
	 * 打开本地或者远程目标文件输入流
	 * 
	 * @param urlFilePath 本地或者远程目标文件
	 * @return InputStream 目标文件输入流
	 * @throws IOException
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-12-31 上午12:26:10
	 * @version V1.0
	 */
	public static InputStream openInputStream(String urlFilePath) throws IOException {
		if(StringUtils.startsWithIgnoreCase(urlFilePath, PROTOCOL_FTP)) {
			//使用FTP协议读取文件
			return ((BaseIOUtil)getInstance().STRATEGY_MAP.get(PROTOCOL_FTP)).openInputStream(urlFilePath);
		} else {
			return ((BaseIOUtil)getInstance().STRATEGY_MAP.get(PROTOCOL_FILE)).openInputStream(urlFilePath);
		}
	}
	
	/**
	 * 创建目录，若上级目录不存在，则首先创建上级目录
	 * 
	 * @param filepath 目录路径
	 * @throws IOException
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2014-11-4 上午09:45:40
	 * @version V1.0
	 */
	public static void mkdir(String filepath) throws IOException {
		mkdir(new File(filepath));
	}
	
	/**
	 * 创建目录，若上级目录不存在，则首先创建上级目录
	 * 
	 * @param file 目录路径
	 * @throws IOException
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2014-11-4 上午09:45:54
	 * @version V1.0
	 */
	public static void mkdir(File file) throws IOException {
		FileUtils.forceMkdir(file);
	}
}
